<!--
  * 日志
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="4">
          <a-form-item label="请求标题" class="smart-margin-bottom0">
            <a-input v-model:value.trim="queryForm.title" placeholder="请求标题" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-form-item label="创建时间" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="queryForm.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'content'">
            <div>
              <JsonViewer :value="text ? JSON.parse(text) : ''" :expand-depth="0" sort theme="jv-dark" />
            </div>
          </template>
          <template v-if="column.dataIndex === 'response'">
            <div class="smart-table-operate">
              <JsonViewer :value="text ? JSON.parse(text) : ''" :expand-depth="0" theme="jv-dark" sort />
            </div>
          </template>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="() => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import { JsonViewer } from 'vue3-json-viewer'
// import 'vue3-json-viewer/dist/index.css'
import { logApi } from '@/api/system/log-api'
import TableOperator from '@/components/support/table-operator/index.vue'
import { useQuery } from '@/hooks/web/use-query-hook'
import { QUERY_FORM_OP } from '@/constants/support/query-form-op-const'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useResize } from '@/hooks/web/use-resize-hook'
// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '操作人',
    dataIndex: ['user', 'username']
  },
  {
    title: '请求方法',
    dataIndex: 'method'
  },
  {
    title: '请求标题',
    dataIndex: 'title'
  },
  {
    title: 'IP地址',
    dataIndex: 'ip'
  },
  {
    title: '路由地址',
    dataIndex: 'url'
  },
  {
    title: '请求数据',
    dataIndex: 'content'
  },
  {
    title: '响应数据',
    dataIndex: 'response'
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async (params) => {
    const res = await logApi.queryList(params)
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {
    title: '',
    create_time: DEFAULT_TIME_RANGE.value
  },
  queryFormOp: {
    title: QUERY_FORM_OP['模糊查询'],
    create_time: QUERY_FORM_OP['范围查询']
  }
})
onMounted(() => {
  queryData()
})
</script>
